<template>
  <div>
    <div>
      <el-input style="width: 150px" placeholder="查询植物名称" v-model="name"></el-input>
      <el-input style="width: 150px;padding-left: 10px" placeholder="查询所属区域" v-model="region"></el-input>
      <el-input style="width: 150px;padding-left: 10px" placeholder="查询植物类型" v-model="type"></el-input>
      <el-input style="width: 150px;padding-left: 10px" placeholder="查询出水量" v-model="waterOutput"></el-input>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary"  plain @click="load(1)">查询</el-button>
      <el-button type="info"  plain @click="reset">重置</el-button>
      <el-button type="success" plain @click="exportData">批量导出</el-button>
      <el-upload action="http://localhost:9090/standard/import" :headers="{token: user.token}" :on-success="handleImport"
                 style="display: inline-block;margin-left: 10px" :show-file-list="false">
        <el-button type="warning" plain >批量导入</el-button>
      </el-upload>
      <el-button type="primary" plain @click="handleAdd" style="margin-left: 10px">新增</el-button>
      <el-button type="danger" plain @click="delBatch">批量删除</el-button>
    </div>
    <el-table :data="tableData" stripe :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666' }" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="id" label="序号" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="植物名称"  align="center"></el-table-column>
      <el-table-column prop="region" label="所属区域"  align="center"></el-table-column>
      <el-table-column prop="type" label="植物类型" align="center"></el-table-column>
      <el-table-column prop="waterOutput" label="出水量" align="center"></el-table-column>
      <el-table-column prop="illumination" label="标准光照" align="center"></el-table-column>
      <el-table-column prop="temperature" label="标准温度" align="center"></el-table-column>
      <el-table-column prop="soilMoisture" label="标准土壤湿度" align="center"></el-table-column>
<!--      <el-table-column prop="standards" label="点检标准"  width="100" align="center">-->
<!--        <template v-slot="scope">-->
<!--          <el-button @click="showContent(scope.row.standards)" size="mini">显示内容</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="standards" label="标准详情"  width="130" align="center">-->
<!--        <template v-slot="scope">-->
<!--          <el-button @click="$router.push('/standardDetail?id=' + scope.row.id)" size="mini">在详情页显示</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="airHumidity" label="标准空气湿度"  width="150" align="center"></el-table-column>
<!--      <el-table-column prop="state" label="设备状态" width="120" align="center">-->
<!--        <template v-slot="scope">-->
<!--          <el-select v-model="scope.row.state" filterable placeholder="请选择" @change="changeOpen(scope.row)">-->
<!--            <el-option-->
<!--                v-for="item in options"-->
<!--                :key="item.value"-->
<!--                :label="item.label"-->
<!--                :value="item.value"-->
<!--                :v-model="scope.row.state">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button size="mini" type="primary" circle @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" circle @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="标准录入" :visible.sync="fromVisible" width="60%" @close="closeDialog" :close-on-click-modal="false">
      <el-form :model="form" label-width="80px" style="padding-right: 20px" :rules="rules" ref="formRef">
        <el-form-item label="植物名称" prop="name">
          <el-input v-model="form.name" placeholder="植物名称"></el-input>
        </el-form-item>
        <el-form-item label="所属区域" prop="region">
          <el-input v-model="form.region" placeholder="所属区域"></el-input>
        </el-form-item>
        <el-form-item label="植物类型" prop="type">
          <el-input v-model="form.type" placeholder="植物类型"></el-input>
        </el-form-item>
        <el-form-item label="出水量" prop="waterOutput">
          <el-input v-model="form.waterOutput" placeholder="出水量"></el-input>
        </el-form-item>
        <el-form-item label="空气湿度" prop="airHumidity">
          <el-input v-model="form.airHumidity" placeholder="空气湿度"></el-input>
        </el-form-item>
        <el-form-item label="标准光照" prop="illumination">
          <el-input v-model="form.illumination" placeholder="标准光照"></el-input>
        </el-form-item>
        <el-form-item label="标准温度" prop="temperature">
          <el-input v-model="form.temperature" placeholder="标准光照"></el-input>
        </el-form-item>
        <el-form-item label="土壤湿度" prop="temperature">
          <el-input v-model="form.soilMoisture" placeholder="标准土壤湿度"></el-input>
        </el-form-item>
<!--        <el-form-item label="标准录入" prop="standards">-->
<!--          <div id="editor"></div>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
<!--    <el-dialog title="设备标准" :visible.sync="fromVisible1" width="60%">-->
<!--      <div class="w-e-text">-->
<!--        <div v-html="standards"></div>-->
<!--      </div>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="fromVisible1 = false">确 定</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->


  </div>
</template>

<script>
import E from "wangeditor"
import hljs from 'highlight.js'
export default {
  name: "Standard",
  data() {
    return {
      value: '',
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 5,  // 每页显示的个数
      name: '',
      soilMoisture:'',
      region:'',
      records:'',
      illumination:'',
      temperature:'',
      waterOutput:'',
      type:'',
      airHumidity: '',
      total: 0,
      fromVisible: false,
      form: {},
      user: JSON.parse(localStorage.getItem('honey-user') || '{}'),
      rules: {
        region: [
          { required: true, message: '请输入所属区域', trigger: 'blur' },
        ]
      },
      ids: [],
      editor: null,
      // standards:'',
      fromVisible1: false,
    }
  },
  created() {
    this.load()
  },
  methods: {
    handleImport(res,file,fileList){
      if (res.code ==='200'){
        this.$message.success("操作成功")
        this.load(1)
      }else {
        this.$message.error(res.msg)
      }
    },
    exportData(){   //批量导出数据
      if (!this.ids.length){  //没有选择行的时候，全部导出，或者根据我的搜索条件导出
        window.open('http://localhost:9090/standard/export?token=' + this.user.token + "&region=" + this.region + "&name=" + this.name)
      } else {   //[1,2,3] => '1,2,3'
        let idStr = this.ids.join(',')
        window.open('http://localhost:9090/standard/export?token=' + this.user.token + '&ids=' + idStr)

      }

    },
    // showContent(standards){
    //   this.standards = standards
    //   this.fromVisible1 = true
    // },
    closeDialog(){
      // 销毁编辑器
      // this.editor.destroy()
      this.editor = null
    },
    delBatch() {
      if (!this.ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('您确认批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/standard/delete/batch', { data: this.ids }).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {})
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
    del(id) {
      this.$confirm('您确认删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/standard/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {})
    },
    // setRichText(){
    //   //富文本编译器
    //
    //   this.$nextTick(() => {
    //     this.editor = new E(`#editor`)
    //     this.editor.highlight = hljs
    //     this.editor.config.uploadImgServer = 'http://localhost:9090/file/editor/upload'
    //     this.editor.config.uploadFileName = 'file'
    //     this.editor.config.uploadImgHeaders = {
    //       token: this.user.token
    //     }
    //     this.editor.config.uploadImgParams = {
    //       type: 'img',
    //     }
    //     this.editor.config.uploadVideoServer = 'http://localhost:9090/file/editor/upload'
    //     this.editor.config.uploadVideoName = 'file'
    //     this.editor.config.uploadVideoHeaders = {
    //       token: this.user.token
    //     }
    //     this.editor.config.uploadVideoParams = {
    //       type: 'video',
    //     }
    //     this.editor.create()  // 创建
    //   })
    // },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
      this.setRichText()
      // setTimeout(() => {
      //   this.editor.txt.html(row.standards)  // 设置富文本内容
      // }, 0)
    },
    handleAdd() {   // 新增数据
      this.form = {}  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗
      // this.setRichText()
    },
    changeOpen(form) {
      // 调用更新的接口  更新数据到数据库
      this.form = JSON.parse(JSON.stringify(form))
      this.sendSaveRequest()   // 直接发请求就可以了
    },
    sendSaveRequest() {
      this.$request({
        url: this.form.id ? '/standard/update': '/standard/add',
        method: this.form.id ? 'PUT' : 'POST',
        data: this.form
      }).then(res => {
        if (res.code === '200') {  // 表示成功保存
          this.$message.success('保存成功')
          this.load(1)
          this.fromVisible = false
        } else {
          this.$message.error(res.msg)  // 弹出错误的信息
        }
      })
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // 获取编辑框的内容
          // let standards = this.editor.txt.html()
          // this.form.standards = standards
          this.sendSaveRequest()
        }
      })
    },
    reset() {
      this.name = ''
      this.region= ''
      this.type = ''
      this.airHumidity = ''
      this.waterOutput= ''
      this.load()
    },
    load(pageNum) {  // 分页查询
      if (pageNum)  this.pageNum = pageNum
      this.$request.get('/standard/selectByPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          region: this.region,
          type: this.type,
          airHumidity: this.airHumidity,
          waterOutput: this.waterOutput
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
  }
}
</script>

<style scoped>

</style>